package com.michaels;

import com.alibaba.fastjson.JSON;
import com.util.FileUtil;
import com.michaels.ifr.constants.SkuStatus;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.*;

public class StatusCompare {
    public static void main(String[] args) throws IOException {
        //List<String> devList = FileUtil.readFile("mik_dev_0501.json");
        //List<String> tst03List = FileUtil.readFile("mik_tst03_0501.json");
        List<String> devList = FileUtil.readFile("/Users/user/A/projects/ifr/dev/mik_dev_0504.json");
        List<String> tst03List = FileUtil.readFile("/Users/user/A/projects/ifr/dev/mik_tst03_0505.json");
        List<String> tst02List = FileUtil.readFile("/Users/user/A/projects/ifr/dev/mik_tst02_0504.json");
        List<String> tst00List = FileUtil.readFile("/Users/user/A/projects/ifr/dev/mik_tst00_0505.json");

        Map<String, ProductStatusInfo> devMap = getMap(devList);
        Map<String, ProductStatusInfo> tst03Map = getMap(tst03List);
        Map<String, ProductStatusInfo> tst02Map = getMap(tst02List);
        Map<String, ProductStatusInfo> tst00Map = getMap(tst00List);

//        exportCsv(devMap, tst03Map, tst02Map, tst00Map);
//        exportDifferentCsv(devMap, tst03Map, tst02Map, tst00Map);
        exportDifferentCsv(tst03Map, tst00Map);
        exportDifferentCsv(tst00Map);

    }


    public static <T> String exportCsv(Map<String, ProductStatusInfo> devMap,
                                       Map<String, ProductStatusInfo> tst03Map,
                                       Map<String, ProductStatusInfo> tst02Map,
                                       Map<String, ProductStatusInfo> tst00Map) throws IOException, IllegalArgumentException {
        List<String> titleList = new ArrayList<String>(){{
                add("sku_number");
                add("dev_status");
                add("tst03_status");
                add("tst02_status");
                add("tst00_status");
        }};

        File file = new File("product_status_compare.csv");
        //构建输出流，同时指定编码
        OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(file), "utf-8");

        //csv文件是逗号分隔，除第一个外，每次写入一个单元格数据后需要输入逗号
        for(String title : titleList){
            ow.write(title);
            ow.write(",");
        }
        //写完文件头后换行
        ow.write("\r\n");

        for(Map.Entry<String, ProductStatusInfo> entry: tst00Map.entrySet()) {
            String skuNumber = entry.getKey();
            ow.write( skuNumber+ ",");

            ProductStatusInfo devProductStatusInfo = devMap.get(skuNumber);
            if(devProductStatusInfo != null) {
                ow.write(devProductStatusInfo.getStatus() + ",");
            } else {
                ow.write( "No this sku,");
            }

            ProductStatusInfo tst03ProductStatusInfo = tst03Map.get(skuNumber);
            if(tst03ProductStatusInfo != null) {
                ow.write(tst03ProductStatusInfo.getStatus() + ",");
            } else {
                ow.write( "No this sku,");
            }

            ProductStatusInfo tst02ProductStatusInfo = tst02Map.get(skuNumber);
            if(tst02ProductStatusInfo != null) {
                ow.write(tst02ProductStatusInfo.getStatus() + ",");
            } else {
                ow.write( "No this sku,");
            }

            ProductStatusInfo tst00ProductStatusInfo = tst00Map.get(skuNumber);
            if(tst00ProductStatusInfo != null) {
                ow.write(tst00ProductStatusInfo.getStatus() + ",");
            } else {
                ow.write( "No this sku,");
            }

            ow.write("\r\n");
        }

//        List<Map<String, String>> devMapList = JSON.parseArray()

        //写内容

        ow.flush();
        ow.close();
        return "0";
    }

    public static <T> String exportDifferentCsv(Map<String, ProductStatusInfo> devMap,
                                       Map<String, ProductStatusInfo> tst03Map,
                                       Map<String, ProductStatusInfo> tst02Map,
                                       Map<String, ProductStatusInfo> tst00Map) throws IOException, IllegalArgumentException {
        List<String> titleList = new ArrayList<String>(){{
            add("sku_number");
            add("dev_status");
            add("tst03_status");
            add("tst02_status");
            add("tst00_status");
        }};

        File file = new File("product_different_status_compare.csv");
        //构建输出流，同时指定编码
        OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(file), "utf-8");

        //csv文件是逗号分隔，除第一个外，每次写入一个单元格数据后需要输入逗号
        for(String title : titleList){
            ow.write(title);
            ow.write(",");
        }
        //写完文件头后换行
        ow.write("\r\n");

        String noThisSku = "No this sku,";
        for(Map.Entry<String, ProductStatusInfo> entry: tst00Map.entrySet()) {
            StringBuilder sb = new StringBuilder();

            Set<String> statusSet = new HashSet<>();

            String skuNumber = entry.getKey();
            sb.append(skuNumber + ",");

            ProductStatusInfo devProductStatusInfo = devMap.get(skuNumber);
            if(devProductStatusInfo != null) {
                sb.append(devProductStatusInfo.getStatus() + ",");
                statusSet.add(String.valueOf(devProductStatusInfo.getStatus()));
            } else {
                sb.append(noThisSku);
                statusSet.add(noThisSku);
            }

            ProductStatusInfo tst03ProductStatusInfo = tst03Map.get(skuNumber);
            if(tst03ProductStatusInfo != null) {
                sb.append(tst03ProductStatusInfo.getStatus() + ",");
                statusSet.add(String.valueOf(tst03ProductStatusInfo.getStatus()));
            } else {
                sb.append(noThisSku);
                statusSet.add(noThisSku);
            }

            ProductStatusInfo tst02ProductStatusInfo = tst02Map.get(skuNumber);
            if(tst02ProductStatusInfo != null) {
                sb.append(tst02ProductStatusInfo.getStatus() + ",");
                statusSet.add(String.valueOf(tst02ProductStatusInfo.getStatus()));
            } else {
                sb.append(noThisSku);
                statusSet.add(noThisSku);
            }

            ProductStatusInfo tst00ProductStatusInfo = tst00Map.get(skuNumber);
            if(tst00ProductStatusInfo != null) {
                sb.append(tst00ProductStatusInfo.getStatus() + ",");
                statusSet.add(String.valueOf(tst00ProductStatusInfo.getStatus()));
            } else {
                sb.append(noThisSku);
                statusSet.add(noThisSku);
            }
            if(statusSet.size() > 1) {
                ow.write(sb.toString());
                ow.write("\r\n");
            }
        }

//        List<Map<String, String>> devMapList = JSON.parseArray()

        //写内容

        ow.flush();
        ow.close();
        return "0";
    }

    public static <T> String exportDifferentCsv(Map<String, ProductStatusInfo> tst03Map,
                                                Map<String, ProductStatusInfo> tst00Map) throws IOException, IllegalArgumentException {
        List<String> titleList = new ArrayList<String>(){{
            add("sku_number");
            add("tst03_status");
            add("tst03_online_inventory_available");
            add("tst00_status");
            add("tst00_online_inventory_available");
        }};

        File file = new File("product_different_status_compare_qa_vs_tst_with_inventory.csv");
        //构建输出流，同时指定编码
        OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(file), "utf-8");

        //csv文件是逗号分隔，除第一个外，每次写入一个单元格数据后需要输入逗号
        for(String title : titleList){
            ow.write(title);
            ow.write(",");
        }
        //写完文件头后换行
        ow.write("\r\n");

        String noThisSku = "No this sku,No this sku,";
        for(Map.Entry<String, ProductStatusInfo> entry: tst00Map.entrySet()) {
            StringBuilder sb = new StringBuilder();

            Set<String> statusSet = new HashSet<>();

            String skuNumber = entry.getKey();
            sb.append(skuNumber + ",");


            ProductStatusInfo tst03ProductStatusInfo = tst03Map.get(skuNumber);
            if(tst03ProductStatusInfo != null) {
                sb.append(tst03ProductStatusInfo.getStatus() + ",");
                sb.append(tst03ProductStatusInfo.getOnlineInventoryAvailable() + ",");
                statusSet.add(String.valueOf(tst03ProductStatusInfo.getStatus()));
            } else {
                sb.append(noThisSku);
                statusSet.add(noThisSku);
            }


            ProductStatusInfo tst00ProductStatusInfo = tst00Map.get(skuNumber);
            if(tst00ProductStatusInfo != null) {
                sb.append(tst00ProductStatusInfo.getStatus() + ",");
                sb.append(tst00ProductStatusInfo.getOnlineInventoryAvailable() + ",");
                statusSet.add(String.valueOf(tst00ProductStatusInfo.getStatus()));
            } else {
                sb.append(noThisSku);
                statusSet.add(noThisSku);
            }
            if(statusSet.size() > 1) {
                ow.write(sb.toString());
                ow.write("\r\n");
            }
        }

//        List<Map<String, String>> devMapList = JSON.parseArray()

        //写内容

        ow.flush();
        ow.close();
        return "0";
    }

    public StatusCompare() {
    }

    public static <T> String exportDifferentCsv(Map<String, ProductStatusInfo> map) throws IOException, IllegalArgumentException {
        List<String> titleList = new ArrayList<String>(){{
            add("sku_number");
            add("status");
            add("online_inventory_available");
        }};

        File file = new File("product_different_status_inventory_compare_tst.csv");
        //构建输出流，同时指定编码
        OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(file), "utf-8");

        //csv文件是逗号分隔，除第一个外，每次写入一个单元格数据后需要输入逗号
        for(String title : titleList){
            ow.write(title);
            ow.write(",");
        }
        //写完文件头后换行
        ow.write("\r\n");

        for(Map.Entry<String, ProductStatusInfo> entry: map.entrySet()) {
            String skuNumber = entry.getKey();
            ProductStatusInfo productStatusInfo = map.get(skuNumber);
            if(productStatusInfo != null) {
                if(SkuStatus.SOLD_OUT.getCode().equals(productStatusInfo.getStatus())) {
                    if(productStatusInfo.getOnlineInventoryAvailable() == null || false != productStatusInfo.getOnlineInventoryAvailable()) {
                        ow.write(skuNumber + ",");
                        ow.write(productStatusInfo.getStatus() + ",");
                        ow.write(productStatusInfo.getOnlineInventoryAvailable() + ",");
                        ow.write("\r\n");
                    }
                }
            }
        }

//        List<Map<String, String>> devMapList = JSON.parseArray()

        //写内容

        ow.flush();
        ow.close();
        return "0";
    }

    public static Map<String, ProductStatusInfo> getMap(List<String> list) {
        Map<String, ProductStatusInfo> map = new HashMap<>();
        for(String str : list){
            ProductStatusInfo productStatusInfo = JSON.parseObject(str, ProductStatusInfo.class);
            map.put(productStatusInfo.getSkuNumber(), productStatusInfo);
        }
        return map;
    }
}
